package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SysUserMapper;
import com.nineclock.system.pojo.SysUser;
import com.nineclock.system.service.SMSService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SMSService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public void createSmsCode(String mobile, String type) {

        if (mobile == null){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //1,判断手机号是否合法
        boolean hkPhoneLegal = PhoneNumCheckUtils.isPhoneLegal(mobile);
        if (!hkPhoneLegal){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //1.3 如果是注册, 判断手机是否已经存在 , 如果存在, 则不允许注册;
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUser::getMobile,mobile);
        SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper);
        if ("2".equals(type)){

            if (sysUser != null){
                throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
            }
        }
        //其他情况 手机号必须存在
        else {
            if (sysUser == null){
                throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
            }
        }

        //2. 生成随机数验证码
        String randomCode = RandomUtil.randomNumbers(6);

        //3.发送验证码到手机
        //        aliyunSmsUtils.sendSMS(mobile, code);
        //4. 存储验证码到redis中
        String key = getPrefix(type) + mobile;
        //4.1 组装redis的key
        //4.2 存入redis,并设置过期时间 5 分钟
        redisTemplate.boundValueOps(key).set(randomCode,5, TimeUnit.MINUTES);
        log.info("成功发送验证码 , 具体信息 : " + mobile+", " + randomCode);

    }

    @Override
    public Boolean verify(String mobile, String checkcode, String type) {
        if (mobile == null || type == null || checkcode == null){
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //获取验证码
        String key = this.getPrefix(type)+mobile;
        String cood = redisTemplate.boundValueOps(key).get();
        if (!cood.equals(checkcode)){
            throw new NcException(ResponseEnum.CODE_IMAGE_ERROR);
        }
        return true;
    }

    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }
    }
}
